﻿Namespace EIB.Bus.USB
    Public MustInherit Class KNXUSB_FrameBase
        Inherits FrameBase

        Protected _reportID As Byte = Nothing

        'PacketInfo
        Protected _sequenceNumber As Byte = Nothing
        Protected _isStartPacket As Boolean = Nothing
        Protected _isEndPacket As Boolean = Nothing
        Protected _isPartialPacket As Boolean = Nothing

        Protected _dataLength As Byte = Nothing

        Protected _protocolVersion As Byte = Nothing
        Protected _headerLength As Byte = Nothing
        Protected _bodyLength As UInt16 = Nothing
        Protected _protocolID As Frame_ProtocolID = Nothing
        Protected _emiID As Frame_EMIType = Nothing
        Protected _manufacturerCode As UInt16 = Nothing

        Protected _USBData As Byte() = Nothing
        Protected _data As Byte() = Nothing

        Sub New(busdata As Byte())
            If Not busdata(0) = &H1 Then Throw New EIB.Exceptions.EIBDataDecodeException("")
            If Not busdata(3) = &H0 Then Throw New EIB.Exceptions.EIBDataDecodeException("")
            If Not busdata(4) = &H8 Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            _USBData = busdata

            'KNX HID Report Header
            _reportID = busdata(0) 'must be 0x01
            _sequenceNumber = (busdata(1) And &HF0) >> 4
            _isStartPacket = (busdata(1) And &H1)
            _isEndPacket = (busdata(1) And &H2) >> 1
            _isPartialPacket = (busdata(1) And &H4) >> 2
            _dataLength = busdata(2)

            'KNX HID Report Body
            _protocolVersion = busdata(3) 'must be 0x00
            _headerLength = busdata(4) 'must be 0x08 for protocolVersion 0x00
            _bodyLength = (busdata(5) << 8) + busdata(6)
            _protocolID = busdata(7)
            _emiID = busdata(8)
            _manufacturerCode = (busdata(9) << 8) + busdata(10)

            _data = New Byte(_bodyLength - 1) {}
            For i = 0 To _bodyLength - 1
                _data(i) = busdata(11 + i)
            Next
        End Sub

        Sub New(reportID As Byte, sequenceNumber As Byte, isStartPacket As Boolean, isEndpacket As Boolean, isPartialPacket As Boolean, protocolVersion As Byte)
            '...

        End Sub




    End Class
End Namespace